package org.ct.gui.panels;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;

import org.ct.gui.buttons.AddStudentButton;
import org.ct.gui.tables.StudentsTable;

@SuppressWarnings("serial")
public class StudentsPanel extends JPanel {
	public StudentsPanel() {
		super();
		setupLayout();
	}

	private void setupLayout() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		StudentsControlPanel controlPanel = new StudentsControlPanel();
		StudentsTablePanel table = new StudentsTablePanel();
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, controlPanel, table);

		add(splitPane);
	}

}

@SuppressWarnings("serial")
class StudentsControlPanel extends JPanel {
	JTextField idField = new JTextField();
	JTextField firstNameField = new JTextField();
	JTextField surnameField = new JTextField();

	JLabel idLabel = new JLabel("ID");
	JLabel firstNameLabel = new JLabel("Imi");
	JLabel surnameLabel = new JLabel("Nazwisko");

	AddStudentButton addStudentButton = new AddStudentButton("Dodaj", idField, firstNameField, surnameField);

	public StudentsControlPanel() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		idField.setMinimumSize(new Dimension(100, 0));
		firstNameLabel.setMinimumSize(new Dimension(100, 0));
		surnameLabel.setMinimumSize(new Dimension(100, 0));
		addComponents();
	}

	private void addComponents() {

		add(new TextFields());
		JPanel buttonPanel = new JPanel(new FlowLayout());
		//		buttonPanel.setLayout(new BorderLayout());
		buttonPanel.add(addStudentButton);
		add(buttonPanel);

	}

	class TextFields extends JPanel {
		public TextFields() {
			GroupLayout layout = new GroupLayout(this);
			setLayout(layout);

			layout.setAutoCreateGaps(true);

			GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
			hGroup.addGroup(layout.createParallelGroup().addComponent(idLabel).addComponent(firstNameLabel)
					.addComponent(surnameLabel));
			hGroup.addGroup(layout.createParallelGroup().addComponent(idField).addComponent(firstNameField)
					.addComponent(surnameField));
			layout.setHorizontalGroup(hGroup);

			GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
			vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(idLabel).addComponent(idField));
			vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(firstNameLabel)
					.addComponent(firstNameField));
			vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE).addComponent(surnameLabel)
					.addComponent(surnameField));

			layout.setVerticalGroup(vGroup);
		}
	}
}

@SuppressWarnings("serial")
class StudentsTablePanel extends JPanel {
	public StudentsTablePanel() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		JScrollPane scrollPane = new JScrollPane(new StudentsTable());
		add(scrollPane);
	}
}